const libcu = require('libcu');
const moment = require('moment');


class Home {
    async getNum() {
        let sign = await instance.dbHandler.connect();
        log.debug('sign = ', sign);
        let sql = 'select num from myweb.home_def limit 1';
        let res = await instance.dbHandler.dbop(sql, sign.data);
        log.debug("res = ", res);
        res.data[0].num++;
        try {
            let update = `update myweb.home_def set num = '${res.data[0].num}' where num = '${res.data[0].num - 1}'`;
            console.log('updater = ', update);
            let updateRes = await instance.dbHandler.dbop(update, sign.data);        //这个不用异步也可以
            log.debug("updateRes = ", updateRes);
        } catch (error) {
            log.error("getNum update error ->", error);
        }

        log.debug("目前网站的访问量为 ", res.data[0].num);
        return res.data[0].num;
    }

    async getHot() {
        let sign = await instance.dbHandler.connect();
        let sql = `select id, name, description, content, avatar, createTime from myweb.hot 
        order by createTime Desc limit 5;`;
        try {
            let res = await instance.dbHandler.dbop(sql, sign.data);
            return res;
        } catch (error) {
            log.error("getHot数据库执行出错 error->", error);
        }

    }

    async getJoke() {
        let sign = await instance.dbHandler.connect();
        console.log('sign = ', sign);
        let sql = `select count(*) from myweb.joke`;
        try {
            let res = await instance.dbHandler.dbop(sql, sign.data);
            log.debug("冷笑话count结果", res);
            if (res.data[0].COUNT > 3) {
                log.debug('11111');
                let randoms = this.getRandom(res.data[0].COUNT);
                log.debug("冷笑话拿到的随机数为 ", randoms);
                let sql_in = '';
                for (let i = 0; i < randoms.length; i++) {
                    sql_in += randoms[i];
                    if (i != randoms.length - 1) {
                        sql_in += ',';
                    }
                }
                let sql_select = `select * from myweb.joke where id in (${sql_in})`;
                log.debug("去数据库拿冷笑话的sql为",sql_select);
                let response = await instance.dbHandler.dbop(sql_select, sign.data);
                console.log("response = ",response);
                let temp = [];
                for (let i = 0; i < 3; i++) {
                    await libcu.tools.sleep(100);
                    console.log("response.data[i].createTime = ",response.data[i].createTime);
                    let data = {
                        createTime:'创建时间:' + moment(response.data[i].createTime,"YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm:ss"),
                        name: 'a joke',
                        description: 'just enjoy my joke',
                        avatar: 'https://picsum.photos/200',
                        content: response.data[i].text,
                    }
                    temp.push(data);
                }
                res = temp;
            } else {
                throw ("数据库冷笑话数量不足");
            }
            log.debug("冷笑话结果为 ", res);
            return res;
        } catch (error) {
            log.error("getJoke数据库执行出错 error->", error);
            return [];
        }
    }

    //获取不同的随机数
    getRandom(length) {
        let res = [];
        try {
            while (1) {
                if (res.length === 3) {
                    break;
                }
                let i = Math.random() * 100000000000000000000000000000;
                i  = i % length;
                //防止出现重复的随机数
                if(!libcu.tools.contains(res,i)) {
                    res.push(i);
                }
            }
        } catch (error) {
            log.error("getRandom error->",error);
            res = [1,2,3];
        }
        
        return res;
    }
}

exports.home = new Home();